<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

// 获取用户积分信息
$uid = $_G['uid'];
$credit = C::t('common_member_count')->fetch($uid);

// 插件卡密
$table_er_kami = C::t('#erling_kami#er_kami');

// 处理表单提交
if(submitcheck('submit')) {
    if(!submitcheck('submit', 1)) {
        showmessage('表单验证失败，请刷新后重试', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 验证验证码
    $verify_code = trim($_POST['seccode']);
    if(!$verify_code) {
        showmessage('请输入验证码', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    if(!check_seccode($verify_code, $_G['seccodedata']['code'])) {
        showmessage('验证码错误', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 检查用户是否登录
    if(!$uid) {
        showmessage('请先登录', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 检查当前用户组是否允许充值
    $allowed_groups = C::t('#erling_kami#config_er_kami')->fetch_by_keys([
        'allowed_groups'
    ]);
    $allowed_groups = unserialize($allowed_groups['allowed_groups']['value']);
    
    $user_group = DB::fetch_first("SELECT groupid FROM %t WHERE uid=%d", array('common_member', $uid));
    if(!in_array($user_group['groupid'], $allowed_groups)) {
        showmessage('您当前用户组不允许充值', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }


    $kami_code = trim($_POST['kami_code']);
    
    // 验证卡密并充值
    $kami_info = $table_er_kami->fetch_by_code($kami_code);
    if(!$kami_info) {
        showmessage('充值卡错误', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    
    // 检查卡密是否失效
    if($kami_info['status'] != 1) {
        showmessage('该充值卡已失效', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }

    // 检查卡密是否已过期
    if($kami_info['expire_time'] < TIMESTAMP) {
        showmessage('该充值卡已过期', '', array(), array('showdialog' => true, 'locationtime' => 3));
        exit;
    }
    
    // // 充值积分
    $extcredits = 'extcredits'.$kami_info['extcredits'];
    //1. 更新用户积分 
    $update_credit = DB::query("UPDATE %t SET $extcredits=$extcredits+%d WHERE uid=%d", 
        array('common_member_count', $kami_info['credit'], $uid));
    // 2. 记录积分日志
    $logid = DB::insert('common_credit_log', array(
        'uid' => $uid,
        'operation' => 'CDC', // 签到操作类型
        'relatedid' => 0,
        'dateline' => time(),
        $extcredits => $kami_info['credit']
    ), true);
    // 3. 记录积分日志详情
    DB::insert('common_credit_log_field', array(
        'logid' => $logid,
        'title' => '卡密充值(插件)',
        'text' => '用户通过卡密充值获得积分'
    ));

    
    // 更新卡密状态
    $update_data = array(
        'uid' => $_G['uid'],
        'status' => 2,
        'use_time' => TIMESTAMP,
        'update_time' => TIMESTAMP
    );
    $table_er_kami->update($kami_info['id'], $update_data);

    

    showmessage('充值成功', 'home.php?mod=spacecp&ac=plugin&op=credit&id=erling_kami:user_kami_recharge', array(), 
        array(
            'showdialog' => true, 
            'locationtime' => 3,
            'alert' => 'right'
        ));
}
